//
//  CHExceptionScreenController.m
//  CarFile
//
//  Created by 陈浩 on 2017/8/2.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHExceptionScreenController.h"
#import "CHExceptionListController.h"
#import "CZDatePicker.h"
#import "CHSectionViewCell.h"
#import "NSDate+Extend.h"
#import "CHLabel.h"
#import "PJ_Model.h"
@interface CHExceptionScreenController ()<UICollectionViewDelegate,UICollectionViewDataSource,CZDatePickerDelegate>
{

    NSString *areaid;
    NSString *levels;
    NSIndexPath *currentIndexpath;
    CGFloat currentHeight;
}

@property (nonatomic ,strong) UILabel *endTimeLabel;
@property (nonatomic ,strong) UIButton *endBtn;
@property (nonatomic ,strong) UICollectionView *collectionView;
@property (nonatomic ,strong) UICollectionView *collectionV;
@property (nonatomic ,strong) NSArray *titles;
@end

@implementation CHExceptionScreenController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"监管筛选";
    self.view.backgroundColor = [UIColor whiteColor];
    self.titles = @[@"一类",@"二类",@"三类"];
    areaid = (NSString *)[Util getValue:@"areaid"];
    levels = nil;
    UIScrollView *mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, UIScreenW, UIScreenH - 40 - 20 - 64)];
    mainScrollView.scrollEnabled = YES;
    [self.view addSubview:mainScrollView];
    
    
    
    UILabel *timeTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, UIScreenW , 40)];
    timeTitleLabel.text = @"      日期选择";
    timeTitleLabel.font = [UIFont systemFontOfSize:14];
    timeTitleLabel.backgroundColor = [UIColor colorWithHexString:@"eeeef0" alpha:1.0f];
    timeTitleLabel.textColor = [UIColor colorWithHexString:@"148ec7" alpha:1.0f];
    [mainScrollView addSubview:timeTitleLabel];
    
    
    UILabel *endTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(22, 45 ,70, 40)];
    endTitleLabel.textColor = [UIColor colorWithHexString:@"333333" alpha:1.0f];
    endTitleLabel.text = @"结束日期:";
    endTitleLabel.font = [UIFont systemFontOfSize:14];
    [mainScrollView addSubview:endTitleLabel];
    
    CHLabel *endTimeLabel = [[CHLabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(endTitleLabel.frame), 50, UIScreenW - CGRectGetMaxX(endTitleLabel.frame) - 10, 30)];
    endTimeLabel.userInteractionEnabled = YES;
    endTimeLabel.text = @"请选择结束日期";
    endTimeLabel.layer.borderColor = [UIColor colorWithHexString:@"999999" alpha:0.3f].CGColor;
    endTimeLabel.layer.borderWidth = 1;
    endTimeLabel.layer.cornerRadius = 3;
    endTimeLabel.layer.masksToBounds = YES;
    endTimeLabel.textColor = [UIColor colorWithHexString:@"333333" alpha:1.0f];
    endTimeLabel.font = [UIFont systemFontOfSize:14];
    endTimeLabel.text = self.endTime;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    [endTimeLabel addGestureRecognizer:tap1];
    [mainScrollView addSubview:endTimeLabel];
    self.endTimeLabel = endTimeLabel;
    
    UIButton *endBtn = [[UIButton alloc] initWithFrame:CGRectMake(endTimeLabel.frame.size.width - 20-10, 10, 20, 10)];
    [endBtn setImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];
    [endTimeLabel addSubview:endBtn];
    currentHeight = CGRectGetMaxY(endTimeLabel.frame)+10;
    
    NSString *usertype = (NSString *)[Util getValue:@"usertype"];
    if (![usertype isEqualToString:@"99"]) {
        UILabel *areaLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, currentHeight, UIScreenW , 40)];
        areaLabel.text = @"      行政区域选择";
        areaLabel.font = [UIFont systemFontOfSize:14];
        areaLabel.backgroundColor = [UIColor colorWithHexString:@"eeeef0" alpha:1.0f];
        areaLabel.textColor = [UIColor colorWithHexString:@"148ec7" alpha:1.0f];
        [mainScrollView addSubview:areaLabel];
        
        
        NSInteger count = [Globle getInstance].datas.count+1;
        
        NSInteger col = count % 4 == 0 ? count / 4 : count / 4 +1;
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumInteritemSpacing = 1;
        flowLayout.itemSize = CGSizeMake((UIScreenW -10*3 - 40) /4, 30);
        UICollectionView *collectionView =  [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(areaLabel.frame), UIScreenW,col *50 +20) collectionViewLayout:flowLayout];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.scrollEnabled = NO;
        collectionView.pagingEnabled = YES;
        collectionView.backgroundColor = [UIColor whiteColor];
        collectionView.showsHorizontalScrollIndicator = NO;
        [collectionView registerClass:[CHSectionViewCell class] forCellWithReuseIdentifier:@"CHSectionViewCell"];
        [mainScrollView addSubview:collectionView];
        self.collectionView = collectionView;
        currentHeight = CGRectGetMaxY(self.collectionView.frame);
    }
    UILabel *ywLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, currentHeight, UIScreenW , 40)];
    ywLabel.text = @"      业务类别";
    ywLabel.font = [UIFont systemFontOfSize:14];
    ywLabel.backgroundColor = [UIColor colorWithHexString:@"eeeef0" alpha:1.0f];
    ywLabel.textColor = [UIColor colorWithHexString:@"148ec7" alpha:1.0f];
    [mainScrollView addSubview:ywLabel];
    
    UICollectionViewFlowLayout *Layout = [[UICollectionViewFlowLayout alloc] init];
    Layout.minimumInteritemSpacing = 1;
    Layout.itemSize = CGSizeMake((UIScreenW -10*3 - 40) /4, 30);
    UICollectionView *collectionV=  [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(ywLabel.frame), UIScreenW,80) collectionViewLayout:Layout];
    collectionV.delegate = self;
    collectionV.dataSource = self;
    collectionV.scrollEnabled = NO;
    collectionV.pagingEnabled = YES;
    collectionV.backgroundColor = [UIColor whiteColor];
    collectionV.showsHorizontalScrollIndicator = NO;
    [collectionV registerClass:[CHSectionViewCell class] forCellWithReuseIdentifier:@"SectionViewCell"];
    [mainScrollView addSubview:collectionV];
    self.collectionV = collectionV;
    
    mainScrollView.contentSize = CGSizeMake(UIScreenW, CGRectGetMaxY(collectionV.frame) +10);
    
    UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(22, UIScreenH - 40-10 - 64, UIScreenW - 44, 40)];
    sureBtn.layer.cornerRadius = 5;
    sureBtn.layer.masksToBounds = YES;
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureBtn setBackgroundColor:[UIColor colorWithHexString:@"148ec7" alpha:1.0f]];
    [sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sureBtn];
    
    
    
    
}

-(NSDate *)getMinDate{
    // 获取代表公历的NSCalendar对象
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 获取当前日期
    NSDate* dt = [NSDate date];
    // 定义一个时间字段的旗标，指定将会获取指定年、月、日、时、分、秒的信息
    unsigned unitFlags = NSCalendarUnitYear |
    NSCalendarUnitMonth |  NSCalendarUnitDay |
    NSCalendarUnitHour |  NSCalendarUnitMinute |
    NSCalendarUnitSecond | NSCalendarUnitWeekday;
    // 获取不同时间字段的信息
    NSDateComponents* comp = [gregorian components: unitFlags
                                          fromDate:dt];
    NSDate *minDate = [NSDate dateFromString:[NSString stringWithFormat:@"%zd-01-01",comp.year-10] formattor:@"YYYY-MM-dd"];
    return minDate;
    
}



-(void)tapGesture:(UITapGestureRecognizer *)tap{
    
    // 创建日期选择器
    CZDatePicker *datePicker = [CZDatePicker datePicker];
    
    datePicker.maxDate = [NSDate date];
    datePicker.minDate = [self getMinDate];
    datePicker.delegate = self;
    // 显示
    [datePicker show];
    
    
}


- (void)datePicker:(CZDatePicker *)pickerView didClickBtnType:(CZKeyboardToolButtonType)type{
    
    if (type == CZKeyboardToolButtonTypeSure) { // 确定
        // 获得触发日期选择器的对象
    
       self.endTimeLabel.text = [NSDate stringFromDate:pickerView.datePicker.date formattor:@"yyyy-MM-dd"];
    
    }
    
}


//  确定
-(void)sureBtnClick{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"areaid"] = areaid;
    dict[@"levels"] = levels;
    dict[@"datetime"] = self.endTimeLabel.text;
    self.popVCBlock(dict);
    [self.navigationController popViewControllerAnimated:YES];

    

}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView == self.collectionView) {
        return  [Globle getInstance].datas.count+1;
    }else{
        return 4;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.collectionView) {
        static NSString *ID = @"CHSectionViewCell";
        CHSectionViewCell *cell = (CHSectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        if (indexPath.row == 0) {
            cell.label.text = @"全部";
            cell.backgroundColor = [UIColor colorWithHexString:@"148ec7" alpha:1.0f];
            cell.label.textColor = [UIColor whiteColor];
            [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
        }else{
            PJ_Model *model =[Globle getInstance].datas[indexPath.row-1];
            cell.label.text = model.areacn;
        }
        return cell;
    }else{
        
        static NSString *ID = @"SectionViewCell";
        CHSectionViewCell *cell = (CHSectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        if (indexPath.row == 0) {
            cell.label.text = @"全部";
            cell.backgroundColor = [UIColor colorWithHexString:@"148ec7" alpha:1.0f];
            cell.label.textColor = [UIColor whiteColor];
            [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
        }else{
            
            cell.label.text =  self.titles[indexPath.row-1];
        }
        return cell;
    }
    
    
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(20, 20, 20,20);
}

-(CGFloat )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section

{
    return 10;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"点击了第%ld组的第%ld个", indexPath.section, indexPath.row);
    
    if (collectionView  == self.collectionView) {
        CHSectionViewCell *cell = (CHSectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        cell.backgroundColor = [UIColor colorWithHexString:@"148ec7" alpha:1.0f];
        cell.label.textColor = [UIColor whiteColor];
        if (indexPath.row == 0) {
            areaid = (NSString *)[Util getValue:@"areaid"];
        }else{
            PJ_Model *model = [Globle getInstance].datas[indexPath.row-1];
            areaid = model.areaid;
        }
    }else{
        CHSectionViewCell *cell = (CHSectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        cell.backgroundColor = [UIColor colorWithHexString:@"148ec7" alpha:1.0f];
        cell.label.textColor = [UIColor whiteColor];
        if (indexPath.row == 0) {
            levels = @"";
        }else{
            levels = [NSString stringWithFormat:@"%.2zd",indexPath.row];
        }
    }
    
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CHSectionViewCell *cell =  (CHSectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.label.textColor = [UIColor colorWithHexString:@"148ec7" alpha:1.0f];
    
    
}

@end
